﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DataObject.Database;
using DataObject.DataAccess;
using System.Net;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Linq;

public partial class Site : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //MembershipUser user = Membership.GetUser(HttpContext.Current.User.Identity.Name);
        //if (!IsPostBack)
        //{
        string url = Request.Url.Segments.Last().ToLowerInvariant();

        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            IList<Reservation> collection = ReservationDAO.CreateInstant().GetReservationByAlarm(DateTime.Today);
            this.lstTask.DataSource = collection;
            this.lstTask.DataBind();

            if (!HttpContext.Current.User.IsInRole("Administrator"))
            {
                this.headerSystemMenu.Visible = false;
                this.SystemNavigationMenu.Items.Clear();

                if (!HttpContext.Current.User.IsInRole("Operator"))
                {
                    this.headerReservationMenu.Visible = false;
                    this.ReservationNavigationMenu.Items.Clear();
                    //this.OperatorNavigationMenu.Items.Clear();
                    if (this.ReportNavigationMenu.Items.Count > 0)
                    {
                        MenuItem[] menuItems = this.ReportNavigationMenu.Items.Cast<MenuItem>().ToArray();
                        Array.ForEach(menuItems, new Action<MenuItem>(delegate(MenuItem item)
                        {
                            if (item.Text.ToLowerInvariant().Contains("operator"))
                                this.ReportNavigationMenu.Items.Remove(item);
                        }));
                    }
                }

                if (!HttpContext.Current.User.IsInRole("Accountant"))
                {
                    MenuItem[] menuItems = this.ReportNavigationMenu.Items.Cast<MenuItem>().ToArray();
                    Array.ForEach(menuItems, new Action<MenuItem>(delegate(MenuItem item)
                    {
                        if (!item.Text.ToLowerInvariant().Contains("operator"))
                            this.ReportNavigationMenu.Items.Remove(item);
                    }));
                }
            }
            else if (HttpContext.Current.User.Identity.Name != "huutai")
            {
                MenuItem[] menuItems = this.SystemNavigationMenu.Items.Cast<MenuItem>().ToArray();
                this.SystemNavigationMenu.Items.Remove(menuItems.LastOrDefault());
            }
        }
        else
        {
            this.upcomingTask.Visible = false;
            this.headerReservationMenu.Visible = false;
            //this.headerOperatorMenu.Visible = false;
            this.headerReportMenu.Visible = false;
            this.headerSystemMenu.Visible = false;
            this.headerUserManual.Visible = false;
            this.ReservationNavigationMenu.Items.Clear();
            //this.OperatorNavigationMenu.Items.Clear();
            this.SystemNavigationMenu.Items.Clear();
            this.ReportNavigationMenu.Items.Clear();
            this.GuideMenu.Items.Clear();

            if (!url.Contains("default.aspx"))
                Response.Redirect("/Default.aspx");
        }

        foreach (MenuItem item in this.ReservationNavigationMenu.Items)
        {
            if (item.NavigateUrl.ToLowerInvariant().Contains(url))
            {
                item.Selected = true;
                return;
            }
        }

        //foreach (MenuItem item in this.OperatorNavigationMenu.Items)
        //{
        //    if (item.NavigateUrl.ToLowerInvariant().Contains(url))
        //    {
        //        item.Selected = true;
        //        return;
        //    }
        //}

        foreach (MenuItem item in this.ReportNavigationMenu.Items)
        {
            if (item.NavigateUrl.ToLowerInvariant().Contains(url))
            {
                item.Selected = true;
                return;
            }
        }

        foreach (MenuItem item in this.SystemNavigationMenu.Items)
        {
            if (item.NavigateUrl.ToLowerInvariant().Contains(url))
            {
                item.Selected = true;
                return;
            }
        }
        //}
        //if (!this.leftMenu.Visible && (
        //    HttpContext.Current.User.IsInRole("Agent") ||
        //    HttpContext.Current.User.IsInRole("Operator") ||
        //    HttpContext.Current.User.IsInRole("Administrator") ||
        //    HttpContext.Current.User.IsInRole("Accountant")))
        //{
        //    this.leftMenu.Visible = true;

        //}
    }
}